
package com.android.finaltracer.ui;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import android.util.Base64;

import com.android.finaltracer.util.FTKey;

public class DesEncrypt {

    private static byte[] iv_key = FTKey.SETTING_DECODE_IV_KEY;

    public static String encrytDES(String encryptString) throws Exception {
        IvParameterSpec zeroIv = new IvParameterSpec(iv_key);
        // SecretKeySpec key = new
        // SecretKeySpec(getDecodeKeyByVersion(version).getBytes(),"DES");
        SecretKeySpec key = new SecretKeySpec(
                FTKey.SETTING_DECODE_KEY_V1.getBytes(), "DES");
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
        byte[] encrypteddData = cipher.doFinal(encryptString.getBytes());

        return Base64.encodeToString(encrypteddData, Base64.DEFAULT);
        // return encrypteddData.toString();
    }

    public static String decryptDES(String decryptString) throws Exception {
        byte[] byteMi = Base64.decode(decryptString, Base64.DEFAULT);
        // byte[] byteMi = decryptString.getBytes();
        IvParameterSpec zeroIv = new IvParameterSpec(iv_key);
        // SecretKeySpec key = new
        // SecretKeySpec(getDecodeKeyByVersion(version).getBytes(),"DES");
        SecretKeySpec key = new SecretKeySpec(
                FTKey.SETTING_DECODE_KEY_V1.getBytes(), "DES");
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, key, zeroIv);
        byte[] decryptedData = cipher.doFinal(byteMi);

        return new String(decryptedData);
    }

    private static String getDecodeKeyByVersion(int version) {
        String result = FTKey.SETTING_DECODE_KEY_V1;
        switch (version) {
            case 1:
                result = FTKey.SETTING_DECODE_KEY_V1;
                break;
            default:
                result = FTKey.SETTING_DECODE_KEY_V1;
        }

        return result;
    }

}
